<script setup>
  import { User, Lock } from '@element-plus/icons-vue'
  import { ref, watch } from 'vue'
  import { userRegisterService, userLoginService } from '@/api/user'
  // 导入用户仓库
  import { useUserStore } from '@/stores'
  import router from '@/router'

  const isShow = ref(true)
  const form = ref()

  // 整个的用于提交的form表单元素
  const fromModle = ref({
    username: '',
    password: '',
    repassword: ''
  })

  // 表单验证规则
  const rules = {
    username: [
      { required: true, message: '输入内容不能为空', trigger: 'blur' },
      {
        min: 1,
        max: 10,
        message: '请输入 1-10 位长度的用户名',
        trigger: 'blur' // 失去焦点的时候触发
      }
    ],
    password: [
      { required: true, message: '输入内容不能为空', trigger: 'blur' },
      {
        pattern: /^\S{6,15}$/,
        message: '请输入 6-15 位非空格字符的密码',
        trigger: 'blur' // 失去焦点的时候触发
      }
    ],
    repassword: [
      { required: true, message: '输入内容不能为空', trigger: 'blur' },
      {
        pattern: /^\S{6,15}$/,
        message: '请输入 6-15位 的非空字符',
        trigger: 'blur' // 失去焦点的时候触发
      },
      {
        validator: (rule, value, callback) => {
          if (fromModle.value.password !== value) {
            callback(new Error('两次密码不一致'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ]
  }

  // 单击注册
  const register = async () => {
    // 进行一次表单验证
    await form.value.validate() // validate() 方法 对表单进行验证
    // 如果成功
    await userRegisterService(fromModle.value)
    isShow.value = true
    ElMessage.success('注册成功')
  }

  // const route
  const userStore = useUserStore()
  // 点击登录
  const login = async () => {
    //进行表单预校验
    await form.value.validate()
    //发送登录请求
    const res = await userLoginService(fromModle.value)
    // console.log(res)
    router.push('article/channel')

    userStore.setToken(res.token)

    // console.log('登录成功', res)
  }

  // 监听isShow是否改变，若改变则清空表单
  watch(isShow, () => {
    fromModle.value.password = ''
    fromModle.value.username = ''
    fromModle.value.repassword = ''
  })
</script>

<template>
  <el-row class="login-page">
    <el-col class="login-bg" :span="12"></el-col>
    <el-col :span="6" :offset="3" class="form">
      <!-- 登录菜单 -->
      <el-form
        :model="fromModle"
        :rules="rules"
        v-if="isShow"
        ref="form"
        size="large"
        autocomplete="false"
      >
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>

        <el-form-item prop="username">
          <el-input
            v-model="fromModle.username"
            :prefix-icon="User"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="fromModle.password"
            :prefix-icon="Lock"
            name="password"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" underline="hover">忘记密码？</el-link>
          </div>
        </el-form-item>

        <el-form-item>
          <el-button @click="login" class="button" type="primary">
            登录
          </el-button>
        </el-form-item>

        <el-form-item>
          <el-link @click="isShow = false">注册 →</el-link>
        </el-form-item>
      </el-form>

      <!-- 注册菜单 -->
      <el-form
        :model="fromModle"
        :rules="rules"
        v-else
        ref="form"
        size="large"
        autocomplate="false"
      >
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>

        <el-form-item prop="username">
          <el-input
            v-model="fromModle.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="fromModle.password"
            :prefix-icon="Lock"
            name="password"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>

        <el-form-item prop="repassword">
          <el-input
            v-model="fromModle.repassword"
            :prefix-icon="Lock"
            name="password"
            type="password"
            placeholder="请再次输入密码"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button class="button" type="primary" @click="register">
            注册
          </el-button>
        </el-form-item>

        <el-form-item>
          <el-link @click="isShow = true">← 返回</el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
  .login-page {
    height: 100vh;

    .login-bg {
      background:
        url('@/assets/logo2.png') no-repeat 60% center / 240px auto,
        url('@/assets/login_bg.jpg') no-repeat center / cover;

      border-radius: 0 20px 20px 0;
    }

    .el-col {
      border-radius: 4px;
      .el-form {
        .el-form-item {
          .button {
            width: 100%;
          }
        }
      }
    }

    .form {
      display: flex;
      flex-direction: column;
      justify-content: center;
      user-select: none; // 禁止用户进行选择
    }

    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
</style>
